package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 新增套餐接口 新增套餐，新增SetmealDish
     */
    @CacheEvict(cacheNames = "setmeal_id",allEntries = true)
    @Transactional
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.addSetmeal(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //需要添加套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.addsetmealDish(setmealDishes);
    }

    /**
     * 分页查询套餐接口
     */
    @Override
    public PageResult getByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> p = setmealMapper.getByPage(setmealPageQueryDTO);
        PageResult pr = new PageResult();
        pr.setTotal(p.getTotal());
        pr.setRecords(p.getResult());
        return pr;
    }

    /**
     * 根据id查询套餐接口
     */
    @Override
    public SetmealVO getById(Long id) {
         SetmealVO setmealVO = setmealMapper.getById(id);
         return setmealVO;
    }

    /**
     * 修改套餐接口
     */
    @CacheEvict(cacheNames = "setmeal_id",allEntries = true)
    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealMapper.updateSetmeal(setmeal);
        List<Long> ids = new ArrayList<>();
        ids.add(setmeal.getId());
        setmealDishMapper.deleteSetmealDishByid(ids);
        setmealDishMapper.addsetmealDish(setmealDishes);
    }

    /**
     * 批量删除套餐接口
     */
    @CacheEvict(cacheNames = "setmeal_id",allEntries = true)
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        Integer count = setmealMapper.getStatusById(ids);
        if (count > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.deleteBatchSetmeal(ids);
        setmealDishMapper.deleteSetmealDishByid(ids);
    }

    /**
     * 启售停售套餐接口
     */
    @CacheEvict(cacheNames = "setmeal_id",allEntries = true)
    @Override
    public void startOrstopSetmeal(Integer status, Long id) {
        if (status == 1){
            List<Long> ids = setmealDishMapper.getIdsBySetmealId(id);
            Integer count = dishMapper.getDisableCountByid(ids);
            if (count > 0){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 用户功能根据分类id查询套餐
     */
    @Cacheable(cacheNames = "setmeal_id",key = "#categoryId")
    @Override
    public List<Setmeal> userSelectById(Long categoryId) {
        return setmealMapper.userselectById(categoryId);
    }

    /**
     * 根据套餐id查询包含的菜品列表
     */
    @Override
    public List<DishItemVO> selectDishBySetmealId(Long id) {
        return setmealMapper.selectDishBySetmealId(id);
    }
}



